package week07

// 917. 仅仅反转字母
// https://leetcode-cn.com/problems/reverse-only-letters/

func reverseOnlyLetters(s string) string {
	buf := []byte(s)
	var left, right = 0, len(buf)-1
	for left < right {
		for left < right && !check(buf[left]) {
			left++
		}
		for left < right && !check(buf[right]) {
			right--
		}
		if left < right {
			buf[left], buf[right] = buf[right], buf[left]
			left++
			right--
		}
	}
	return string(buf)
}

func check(byt byte) bool {
	return byt >= 'a' && byt <= 'z' || byt >= 'A' && byt <= 'Z'
}
